import { SendOutlined } from "@ant-design/icons"
import { ProCard, ProList } from "@ant-design/pro-components"
import { BetaSchemaForm } from "@ant-design/pro-form"
import { SysUser } from "@sker/model"
import { toWebUrl, useActiveKey, useAxiosWebPost, useCount, useCtx, useModalForm, useModel } from "@sker/sdk"
import { Button, Modal } from "antd"
import { delay } from "./delay"
import { SkerUploadOne } from './components/upload-one'
export const MineMessage: React.FC<{}> = ({ }) => {
    const { currentUser } = useCtx()
    const model = useModel(SysUser)
    const uploadUrl = toWebUrl('imeepos/core/admin/upload')
    const { fetch: sendMessage } = useAxiosWebPost('imeepos/core/admin/send')
    const { count, setCount } = useCount()
    const { activeKey, setActiveKey } = useActiveKey(`system`)

    const { modalProps, formProps, form, setOpen } = useModalForm({
        title: '发送消息',
        columns: async () => {
            const allUser = await model.findAll(['username', 'realname', { company: ['title'] }]).then(res => res.data)
            const userOptions: any = {}
            allUser.map((user: any) => {
                userOptions[user.username] = `${user.realname || user.company?.title}-${user.username || ''}`;
            })
            setTimeout(async () => {
                form.setFieldValue('from_username', currentUser.username)
            }, 100)
            return [
                { dataIndex: 'from_username', formItemProps: { hidden: true } },
                { dataIndex: 'to_username', title: '发送给', valueEnum: userOptions, valueType: 'select' },
                {
                    dataIndex: 'type',
                    title: '类型',
                    valueEnum: {
                        'news': '图文'
                    },
                    valueType: 'select'
                },
                {
                    name: ['type'],
                    valueType: 'dependency',
                    columns: ({ type }) => {
                        if (type === 'news') {
                            return [
                                { title: '标题', dataIndex: 'title' },
                                {
                                    title: '缩略图', dataIndex: 'cover', renderFormItem(schema, config, form, action) {
                                        return <SkerUploadOne value={[]} action={uploadUrl} onUpload={values => {
                                            form.setFieldValue('cover', values)
                                        }} />
                                    },
                                },
                                { title: '内容', dataIndex: 'content', valueType: 'textarea' },
                            ]
                        }
                        return [];
                    }
                }
            ]
        },
        onFinish: async (values) => {
            const { from_username, to_username, type, ...body } = values;
            if (type === 'news') {
                sendMessage({
                    from_username, to_username, type, body
                })
            }
            setOpen(false)
            setCount()
        },
    })
    const children = <ProList params={{
        count,
        activeKey
    }} request={async () => {
        await delay(1000)
        return {}
    }} />
    return <>
        <Modal {...modalProps}>
            <BetaSchemaForm {...formProps} />
        </Modal>
        <ProCard title={'我的消息'} extra={<Button icon={<SendOutlined />} type={'primary'} onClick={e => setOpen(true)}>发送消息</Button>} tabs={{
            activeKey: activeKey,
            onChange: setActiveKey,
            items: [
                {
                    label: '系统消息',
                    key: 'system',
                    children
                },
                {
                    label: '站内邮件',
                    key: 'personal',
                    children
                }
            ]
        }}></ProCard>
    </>
}